package com.zxy.day0624_util.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: lenovo
 * @Date: 2021/9/27 20:53
 * @Description:
 */
public class StreamUtil {
    public StreamUtil() {
    }

    public static void closings(AutoCloseable... closeables) throws Exception {
        if (closeables != null) {
            AutoCloseable[] var1 = closeables;
            int var2 = closeables.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                AutoCloseable auto = var1[var3];
                if (auto != null) {
                    auto.close();
                }
            }
        }

    }

    private static void copy(InputStream inputStream, OutputStream outputStream, boolean closeInputStream, boolean closeOutputStream) throws Exception {
        byte[] b = new byte[1024];
        boolean var5 = false;

        int len;
        while((len = inputStream.read(b)) > 0) {
            outputStream.write(b, 0, len);
        }

        if (closeInputStream) {
            inputStream.close();
        }

        if (closeOutputStream) {
            outputStream.close();
        }

    }

    public static void copy(InputStream inputStream, OutputStream outputStream) throws Exception {
        copy(inputStream, outputStream, false, false);
    }

    public static String asString(InputStream inputStream, String encode, boolean isClose) throws Exception {
        StringBuffer sb = new StringBuffer();
        byte[] b = new byte[1024];
        boolean var5 = false;

        int len;
        while((len = inputStream.read(b)) > 0) {
            sb.append(new String(b, 0, len, encode));
        }

        if (isClose) {
            closings(inputStream);
        }

        return sb.toString();
    }

    public static String asString(InputStream inputStream, boolean isClose) throws Exception {
        return asString(inputStream, "UTF-8", isClose);
    }

    public static String asString(InputStream inputStream) throws Exception {
        return asString(inputStream, true);
    }

    public static String asString(File textFile) throws Exception {
        return asString((InputStream)(new FileInputStream(textFile)));
    }

    public static String asString(File textFile, boolean isClose) throws Exception {
        return asString((InputStream)(new FileInputStream(textFile)), isClose);
    }

    public static String asString(File textFile, String encode, boolean isClose) throws Exception {
        return asString((InputStream)(new FileInputStream(textFile)), encode, isClose);
    }

    public static List<String> readingLineFormTextFile(File textFile, String encode) throws Exception {
        List<String> list = new ArrayList();
        FileReader fr = new FileReader(textFile);
        BufferedReader br = new BufferedReader(fr);
        String str = null;

        while((str = br.readLine()) != null) {
            list.add(str);
        }

        closings(br);
        closings(fr);
        return list;
    }

    public static List<String> readingLineFormTextFile(File textFile) throws Exception {
        return readingLineFormTextFile(textFile, "UTF-8");
    }
}
